<template>
  <Portlet title="提货地址管理" style="position:relative;">
    <DialogAddress :visible.sync="visible" :id="rowId" @refresh="getAddressData"></DialogAddress>
    <el-form label-position="left" label-width="80px">
      <el-row v-if="addressDataArr.length == 0">
        <el-col :span="24">
          你还没有提货地址
        </el-col>
      </el-row>
      <el-row v-else v-for="(item, index) in addressDataArr" :key="index">
        <el-col :span="5">
          <el-form-item label="提货联系:">
            <span>{{item.deliveryName}}</span> <span v-if="item.isDefault == 1"
                                                     class="font-red"> &nbsp;&nbsp;&nbsp;默认</span>
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item label="提货电话:">
            <span>{{item.deliveryPhone}}</span>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="提货地址:">
            <span> {{item.deliveryProvince}} {{item.deliveryCity}} {{item.deliveryDistrict}} {{item.deliveryAddress}}</span>
          </el-form-item>
        </el-col>
        <el-col :span="1">
          <el-form-item>
            <el-button type="text" @click.prevent="addNew(item.id)"> 编辑</el-button>
          </el-form-item>
        </el-col>
        <el-col :span="1">
          <el-form-item>
            <el-button type="text" @click.prevent="showDelModal(item.id, 'del')"> 删除</el-button>
          </el-form-item>
        </el-col>
        <el-col :span="2">
          <el-form-item>
            <el-button type="text" :disabled="item.isDefault == 1" @click.prevent="showDelModal(item.id, 'setDefault')">
              设为默认
            </el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div style="position:absolute;top:5px;right:20px;">
      <el-button type="primary" @click.prevent="addNew()"> 添加新的地址</el-button>
    </div>
  </Portlet>
</template>
<script>
import DialogAddress from '@/views/oms/pickAddress/DialogAddress'
import {confirm} from '@/utils'

export default {
  name: 'pickAddressIndex',
  components: {
    DialogAddress
  },
  data() {
    return {
      rowId: '',
      visible: false,
      addressDataArr: []
    }
  },
  created() {
    this.getAddressData()
  },
  methods: {
    getAddressData() {
      this.$ajax.get('/ms-warehouse-order/chDefaultPickupInfo/getPickupInfoList').then(response => {
        if (response.success) {
          if (Array.isArray(response.data)) {
            this.addressDataArr = response.data || []
          }
        }
      })
    },
    // 新增和修改地址（有id为修改）
    addNew(id) {
      if (id) {
        this.rowId = id
      } else {
        this.rowId = ''
      }
      this.visible = true
    },
    // 删除、设为默认
    showDelModal(id, type) {
      let url = ''
      let title = ''
      if (type === 'del') {
        title = '你确定要删除该地址吗?'
        url = '/ms-warehouse-order/chDefaultPickupInfo/deletePickupInfo'
      } else {
        title = '<p>你确定将改地址设为默认地址吗?</p><p>（录单时将自动填充此地址）</p>'
        url = '/ms-warehouse-order/chDefaultPickupInfo/editDefaultPickupInfo'
      }
      confirm(title, '温馨提示', {
        confirmButtonLoading: true,
        type: 'warning',
        center: true,
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        dangerouslyUseHTMLString: true,
        confirmCallBack: ({confirmButtonLoadingClose, close}) => {
          return this.$ajax.post(url, {id: id}).then(response => {
            if (response.success) {
              close().then(_ => {
                this.$message({
                  type: 'success',
                  message: '操作已完成!'
                })
                this.getAddressData()
              })
            } else {
              confirmButtonLoadingClose()
            }
          }).finally(_ => {
            confirmButtonLoadingClose()
          })
        }
      })
    }
  }
}
</script>
